﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class logs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int userid = -1;
        try
        {
            userid = Convert.ToInt32(Session["userid"]);
        }
        catch (Exception)
        {
            userid = -1;
            Session["userid"] = -1;
        }
        if (userid <= 0)
        {
            Response.Redirect("~/login.aspx");
        }
        if (!Page.IsPostBack)
        {
            Bank.User _user = new Bank.User();
            Label1.Text =  _user.getusername(userid);
            Bank.Balance _balance = new Bank.Balance();
            DataSet ds = _balance.GetTransactionLogs(userid, Request.QueryString["from"], Request.QueryString["to"], Request.QueryString["type"]);

            DataView view = ds.Tables[0].DefaultView;

            GridView1.DataSource = view;
            GridView1.DataBind();

            ViewState["SortData"] = "LogId";
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        e.Cancel = true;
        if (ViewState["SortData"] == null)
        {
            ViewState["SortData"] = e.SortExpression;
        }
        if (ViewState["SortData"].ToString() == e.SortExpression)
        {
            ViewState["SortData"] = ViewState["SortData"].ToString() + " DESC";
        }
        else
        {
            ViewState["SortData"] = e.SortExpression;
        }

        Bank.Balance _balance = new Bank.Balance();
        //DataSet ds = _balance.GetTransactionLogs(Convert.ToInt32(Session["userid"]));
        DataSet ds = _balance.GetTransactionLogs(Convert.ToInt32(Session["userid"]), Request.QueryString["from"], Request.QueryString["to"], Request.QueryString["type"]);

        DataView view = ds.Tables[0].DefaultView;

        view.Sort = ViewState["SortData"].ToString();

        GridView1.DataSource = view;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Bank.Balance _balance = new Bank.Balance();
        //DataSet ds = _balance.GetTransactionLogs(Convert.ToInt32(Session["userid"]));
        DataSet ds = _balance.GetTransactionLogs(Convert.ToInt32(Session["userid"]), Request.QueryString["from"], Request.QueryString["to"], Request.QueryString["type"]);
        DataView view = ds.Tables[0].DefaultView;

        view.Sort = ViewState["SortData"].ToString();

        GridView1.DataSource = view;

        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string type;
        DateTime fromDay, toDay;
        type = DropDownList1.SelectedValue;

        try
        {
            fromDay = new DateTime(Convert.ToInt32(TextBox3.Text), Convert.ToInt32(TextBox2.Text), Convert.ToInt32(TextBox1.Text));
        }
        catch (Exception)
        {
            fromDay = new DateTime(1754,1,1);
        }
        try
        {
            toDay = new DateTime(Convert.ToInt32(TextBox6.Text), Convert.ToInt32(TextBox5.Text), Convert.ToInt32(TextBox4.Text));
        }
        catch (Exception)
        {
            toDay = new DateTime(1754, 1, 1);
        }
        
        string url = Request["URL"] + "?type="+type;
        if (fromDay.Year > 2000)
        {
            url = url + "&from=" + fromDay.ToString();
        }
        if (toDay.Year > 2000)
        {
            url = url + "&to=" + toDay.ToString();
        }

        Response.Redirect(url);
    }
}
